package admin

import (
	"net/http"

	"server/internal/logic/admin"
	"server/internal/svc"
	"server/internal/types"

	tojson "gitee.com/ruikc/tojson"
	"github.com/zeromicro/go-zero/rest/httpx"

	validated "gitee.com/ruikc/validate"
)

func CategoryDelHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.IdJson

		//解析参数，这里要判断错误，否则有的数据解析不出来
		err := httpx.Parse(r, &req)
		if err != nil {
			httpx.OkJsonCtx(r.Context(), w, tojson.Error(err))
			return
		}

		//添加validate表单验证
		err = validated.ValidateStruct(&req)
		if err != nil {
			httpx.OkJsonCtx(r.Context(), w, tojson.Error(err))
			return
		}

		l := admin.NewCategoryDelLogic(r.Context(), svcCtx)
		resp, err := l.CategoryDel(&req)
		if err != nil {
			httpx.OkJsonCtx(r.Context(), w, tojson.Error(err))
		} else {
			httpx.OkJsonCtx(r.Context(), w, tojson.Success(resp))
		}
	}
}
